package com.lcz.googleplay.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.lcz.googleplay.activity.DetailActivity;
import com.lcz.googleplay.adapter.HomeAdapter;
import com.lcz.googleplay.config.HomeProtocol;
import com.lcz.googleplay.domain.HomeInfo;
import com.lcz.googleplay.holder.HomePictureHolder;
import com.lcz.googleplay.utils.GsonUtils;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.utils.ViewUtils;
import com.lcz.googleplay.widget.LoadingFrameLayout;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import java.util.List;

/**
 *
 * Created by hp on 2016/10/25.
 */

public class HomeFragment extends BaseFragment {

    private HomeInfo homeInfo;
    private List<HomeInfo.ListBean> listBean;
    private List<String> picture;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        show();
    }

    @Override
    public View createSuccessView() {
        /*广告图片初始化*/
        HomePictureHolder homePictureHolder = new HomePictureHolder();
        homePictureHolder.setData(picture);
        View view = homePictureHolder.getView();
        //HomePictureHolder中已经指定view的宽高
//        view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ListView listView = new ListView(UIUtils.getActivity());
        listView.addHeaderView(view);
        listView.setAdapter(new HomeAdapter(listBean, listView) {
            @Override
            protected List<HomeInfo.ListBean> onload() {
                HomeProtocol homeProtocol = new HomeProtocol();

                String json = homeProtocol.load(listBean.size());
                System.out.println("HomeFragment.onload====" + json);
                HomeInfo newHomeInfo = GsonUtils.getInstanceByJson(json, HomeInfo.class);
                listBean.addAll(newHomeInfo.getList());
                return newHomeInfo.getList();
            }

            @Override
            public void onInnerItemClick(int position) {
                super.onInnerItemClick(position);
//                ToastUtils.show("position:" + listBean.get(position).getName());
                Intent intent = new Intent(UIUtils.getActivity(), DetailActivity.class);
                intent.putExtra("packageName", listBean.get(position).getPackageName());
                startActivity(intent);
            }
        });
        // 第二个参数 慢慢滑动的时候是否加载图片 false  加载   true 不加载
        //  第三个参数  飞速滑动的时候是否加载图片  true 不加载
        listView.setOnScrollListener(new PauseOnScrollListener(ViewUtils.getImageLoader(), false, true));
        return listView;
    }

    @Override
    public LoadingFrameLayout.LoadResult load() {
        HomeProtocol homeProtocol = new HomeProtocol();
        String json = homeProtocol.load(0);
        homeInfo = GsonUtils.getInstanceByJson(json, HomeInfo.class);
        picture = homeInfo.getPicture();
        listBean = homeInfo.getList();
        return checkData(listBean);
    }


}
